//
//  userViewController.m
//  ui豆瓣
//
//  Created by lirui on 15/12/23.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "userViewController.h"
#import "userTableViewCell.h"
#import "dengluViewController.h"
#import "wodeDetailViewController.h"
#import "wodemovieViewController.h"
#import "DataBaseManger.h"
@interface userViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic, retain)NSMutableArray *userArr;

@end

@implementation userViewController
-(void)viewWillAppear:(BOOL)animated
{ NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"states"];
    if ([states isEqualToString:@"yes"]) {
        self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(zhuxiao:)];
    }else
    {
        self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(denglu:)];
    }

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.tableview = [[ UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = [ UIColor whiteColor];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    [self.tableview registerClass:[ userTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.title = @"我的";
    _userArr =[ @[@"我的活动", @"我的电影", @"清除缓存"] mutableCopy];
    
      [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg.png"]forBarMetrics:UIBarMetricsDefault];
   
    UIButton *denglubutton = [ UIButton buttonWithType:UIButtonTypeCustom];
    denglubutton.frame =CGRectMake(0, 0, 50,50);
    [denglubutton setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    [ denglubutton setTitle:@"登录" forState:UIControlStateNormal];
    [denglubutton addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:denglubutton];
    self.navigationItem.rightBarButtonItem = button;
    
    
}
-(void)denglu
{
    dengluViewController *denglu =[[ dengluViewController alloc] init];
   UINavigationController *navi = [[ UINavigationController alloc] initWithRootViewController:denglu];
    
    [self presentViewController: navi animated:YES completion:^{
        //NSLog(@"登录模态");
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _userArr.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    userTableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.myActivity.text =_userArr[indexPath.row];
//    cell.myMovie.text = @"我的电影";
//    cell.myremove.text = @"清除缓存";
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
    
}
//跳转到登录界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if((indexPath.row == 0))
{
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"states"];
    if ([states isEqualToString:@"yes"]) {
        wodeDetailViewController *wode =[[ wodeDetailViewController alloc] init];
        [self.navigationController pushViewController:wode animated:YES];
        
    }
    else
    {
    dengluViewController *enter = [[ dengluViewController alloc] init];
    UINavigationController *navi = [[ UINavigationController alloc] initWithRootViewController:enter];
    [self presentViewController:navi animated:YES completion:^{
        NSLog(@"模态");
    }];
    }
}else if(indexPath.row == 1)
{
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"states"];
    if ([states isEqualToString:@"yes"]) {
       wodemovieViewController *wodemovie =[[ wodemovieViewController alloc] init];
        [self.navigationController pushViewController:wodemovie animated:YES];
        
    }else{
    dengluViewController *enter = [[ dengluViewController alloc] init];
    UINavigationController *navi = [[ UINavigationController alloc] initWithRootViewController:enter];
    [self presentViewController:navi animated:YES completion:^{
        NSLog(@"模态");
    }];

    }
}
    else
    {
        
        NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"states"];
        if ([states isEqualToString:@"yes"]){
       // NSLog(@"清除");
        NSString *title = NSLocalizedString(@"提示", nil);
        NSString *message = NSLocalizedString(@"是否确认注销", nil);
        NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
        NSString *otherButtonTitle = NSLocalizedString(@"确定", nil);
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        // 取消按钮.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"清除");
        }];
        //确认按钮
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"确认");
            NSString *path =[ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"a.plist"];
            NSMutableArray *arr =[ NSMutableArray array];
            arr =[ NSKeyedUnarchiver unarchiveObjectWithFile:path];
            [arr removeAllObjects];
              [NSKeyedArchiver archiveRootObject:arr toFile:path];
            
            DataBaseManger *dbmanger =[ DataBaseManger shareInstance ];
            [dbmanger openDB];
            [[ DataBaseManger  shareInstance] dropTable];
            
            [self.tableview reloadData];
        }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        }
        else

    {
        UIAlertController *a = [UIAlertController alertControllerWithTitle:@"提示" message:@"未登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *b = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
            
        }];
        UIAlertAction *c = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"确定");
           
            
            
        }];
        
        [a addAction:b];
        [a addAction:c];
        [self presentViewController:a animated:YES completion:^{
            
        }];
     
        //2秒后调用diss
        //参数1: 执行的方法
        //参数2: 执行方法传递的参数
        //参数3: 延时
       // [self performSelector:@selector(diss) withObject:self afterDelay:2];
    }
    }
}
-(void)diss
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)zhuxiao:(UINavigationItem *)zhuxiao
{
    [[NSUserDefaults standardUserDefaults]setObject:@"no" forKey:@"states"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"注销成功");
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登入" style:UIBarButtonItemStylePlain target:self action:@selector(denglu:)];
}
-(void)denglu:(UINavigationItem *)denglu
{  dengluViewController *enter = [[ dengluViewController alloc] init];
    UINavigationController *navi = [[ UINavigationController alloc] initWithRootViewController:enter];
    [self presentViewController:navi animated:YES completion:^{
        NSLog(@"模态");
    }];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
